package com.example.erp;

import org.springframework.ai.audio.transcription.AudioTranscriptionPrompt;
import org.springframework.ai.audio.transcription.AudioTranscriptionResponse;
import org.springframework.ai.openai.OpenAiAudioTranscriptionModel;
import org.springframework.ai.openai.OpenAiAudioTranscriptionOptions;
import org.springframework.ai.openai.api.OpenAiAudioApi;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;

/**
 * 语音转文字
 */
@RestController
@RequestMapping("/openai")
public class Audio2TextController {

	@Resource
	private OpenAiAudioTranscriptionModel transcriptionModel;

	@GetMapping("/audio2text")
	public void testTranscription1() {
		String res = transcriptionModel.call(new ClassPathResource("testai1.mp3"));
		System.out.println(res);
	}

	@GetMapping("/audio2text2")
	public void testTranscription2() {
		// 创建模型属性，采用 whisper-1 语音模型
		OpenAiAudioTranscriptionOptions options = OpenAiAudioTranscriptionOptions.builder()
				.model(OpenAiAudioApi.WhisperModel.WHISPER_1.getValue()).build();

		AudioTranscriptionResponse res = transcriptionModel
				.call(new AudioTranscriptionPrompt(new ClassPathResource("testai22.mp3"), options));
		// 获取翻译内容
		String output = res.getResult().getOutput();
		System.out.println(output);
	}
}
